﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace EasyCheque.Cheque
{
    public partial class ManageCheque : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["uid"] == null)
            {
                Response.Redirect("~/Default.aspx");
                return;
            }
            SqlDataSource_ManageCheque.SelectCommand = "SELECT * FROM [CHEQUE_INFO] WHERE STATE <> 2 AND PAY_ID = " + Session["uid"] + " ";
            for (int i = 0; i < GridView_Cheques.Rows.Count; i++ )
            {
                if (GridView_Cheques.Rows[i].Cells[7].Text == "1")
                {
                    GridView_Cheques.Rows[i].Cells[7].Text = "已使用";
                    GridView_Cheques.Rows[i].Cells[8].Enabled = false;
                    GridView_Cheques.Rows[i].Cells[9].Enabled = false;
                }
                else
                    GridView_Cheques.Rows[i].Cells[7].Text = "可用";
            }
        }

        protected void GridView_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
//                 DataControlLinkButton bt = (DataControlLinkButton)e.Row.Cells[8].Controls[0];
//                 bt.Enabled = false;
//                 bt = (Button)e.Row.Cells[8].Controls[0];
//                 bt.Enabled = false;
            }
        } 

        protected void GridView_Cheques_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            switch (e.CommandName)
            {
                case "Command_DropCheque":
                    {
                        int theIndex = Convert.ToInt32(e.CommandArgument);
                        GridView grid = (GridView)e.CommandSource;
                        GridViewRow row = grid.Rows[theIndex];
                        string theChequeID = row.Cells[0].Text;
                        System.Diagnostics.Debug.WriteLine("CHEQUE_ID: " + theChequeID);
                        System.Diagnostics.Debug.WriteLine("Command_DropCheque");

                        Session["chequeIDToDrop"] = theChequeID;
                        Response.Redirect("~/Cheque/ChequeDrop.aspx");
                        break;
                    }
                case "Command_ResetPasswordCheque":
                    {
                        int theIndex = Convert.ToInt32(e.CommandArgument);
                        GridView grid = (GridView)e.CommandSource;
                        GridViewRow row = grid.Rows[theIndex];
                        string theChequeID = row.Cells[0].Text;
                        System.Diagnostics.Debug.WriteLine("CHEQUE_ID: " + theChequeID);
                        System.Diagnostics.Debug.WriteLine("Command_ResetPasswordCheque");

                        Session["chequeIDToResetPassword"] = theChequeID;
                        Response.Redirect("~/Cheque/ChequeResetPassword.aspx");
                        break;
                    }
            }
        }
    }
}
